﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    /// <summary>
    /// 数据访问类:BillingDetails
    /// </summary>
    public class BillingDetails
    {
        public BillingDetails()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BillingDetailsID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@BillingDetailsID", SqlDbType.Int,4)};
            parameters[0].Value = BillingDetailsID;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "BillingDetails_Exists", parameters);
            roleId = (int)parameters[0].Value;
            if (roleId > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(Model.BillingDetails model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@BillingDetailsID", SqlDbType.Int,4),
					new SqlParameter("@IntoSectionID", SqlDbType.Int,4),
					new SqlParameter("@CustomerID",SqlDbType.Int,4),
					new SqlParameter("@IntoSectionDate", SqlDbType.DateTime),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@IntoSectionType", SqlDbType.NVarChar,50),
					new SqlParameter("@ImportBank", SqlDbType.NVarChar,150),
					new SqlParameter("@ImportBankAccount", SqlDbType.NVarChar,150),
					new SqlParameter("@CollectionType", SqlDbType.NVarChar,50),
					new SqlParameter("@ReceivingBank", SqlDbType.NVarChar,150),
					new SqlParameter("@ReceivingBankAccount", SqlDbType.NVarChar,150),
					new SqlParameter("@ReceivingBankCom", SqlDbType.NVarChar,50),
					new SqlParameter("@ImportAmount", SqlDbType.Decimal,17),
					new SqlParameter("@EndBookDate", SqlDbType.DateTime),
					new SqlParameter("@InputUserID", SqlDbType.Int,4),
					new SqlParameter("@IntoSectionContent", SqlDbType.NVarChar,50)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.IntoSectionID;
            parameters[2].Value = model.CustomerID;
            parameters[3].Value = model.IntoSectionDate;
            parameters[4].Value = model.InpuTime;
            parameters[5].Value = model.IntoSectionType;
            parameters[6].Value = model.ImportBank;
            parameters[7].Value = model.ImportBankAccount;
            parameters[8].Value = model.CollectionType;
            parameters[9].Value = model.ReceivingBank;
            parameters[10].Value = model.ReceivingBankAccount;
            parameters[11].Value = model.ReceivingBankCom;
            parameters[12].Value = model.ImportAmount;
            parameters[13].Value = model.EndBookDate;
            parameters[14].Value = model.InputUserID;
            parameters[15].Value = model.IntoSectionContent;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "BillingDetails_ADD", parameters);
            roleId = (int)parameters[0].Value;
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public bool Update(Model.BillingDetails model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@BillingDetailsID", SqlDbType.Int,4),
					new SqlParameter("@IntoSectionID", SqlDbType.Int,4),
					new SqlParameter("@CustomerID",SqlDbType.Int,4),
					new SqlParameter("@IntoSectionDate", SqlDbType.DateTime),
					new SqlParameter("@InpuTime", SqlDbType.DateTime),
					new SqlParameter("@IntoSectionType", SqlDbType.NVarChar,50),
					new SqlParameter("@ImportBank", SqlDbType.NVarChar,150),
					new SqlParameter("@ImportBankAccount", SqlDbType.NVarChar,150),
					new SqlParameter("@CollectionType", SqlDbType.NVarChar,50),
					new SqlParameter("@ReceivingBank", SqlDbType.NVarChar,150),
					new SqlParameter("@ReceivingBankAccount", SqlDbType.NVarChar,150),
					new SqlParameter("@ReceivingBankCom", SqlDbType.NVarChar,50),
					new SqlParameter("@ImportAmount", SqlDbType.Decimal,17),
					new SqlParameter("@EndBookDate", SqlDbType.DateTime),
					new SqlParameter("@InputUserID", SqlDbType.Int,4),
					new SqlParameter("@IntoSectionContent", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.BillingDetailsID;
            parameters[1].Value = model.IntoSectionID;
            parameters[2].Value = model.CustomerID;
            parameters[3].Value = model.IntoSectionDate;
            parameters[4].Value = model.InpuTime;
            parameters[5].Value = model.IntoSectionType;
            parameters[6].Value = model.ImportBank;
            parameters[7].Value = model.ImportBankAccount;
            parameters[8].Value = model.CollectionType;
            parameters[9].Value = model.ReceivingBank;
            parameters[10].Value = model.ReceivingBankAccount;
            parameters[11].Value = model.ReceivingBankCom;
            parameters[12].Value = model.ImportAmount;
            parameters[13].Value = model.EndBookDate;
            parameters[14].Value = model.InputUserID;
            parameters[15].Value = model.IntoSectionContent;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "BillingDetails_Update", parameters);
            if (intResult > 0)
                return true;
            else
                return false;

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BillingDetailsID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@BillingDetailsID", SqlDbType.Int,4)};
            parameters[0].Value = BillingDetailsID;

            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "BillingDetails_Delete", parameters);
            if (intResult > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string BillingDetailsIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BillingDetails ");
            strSql.Append(" where BillingDetailsID in (" + BillingDetailsIDlist + ")  ");

            SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), null);

            int rows = SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), null);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.BillingDetails GetModel(int BillingDetailsID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@BillingDetailsID", SqlDbType.Int,4)};
            parameters[0].Value = BillingDetailsID;

            Model.BillingDetails model = new Model.BillingDetails();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "BillingDetails_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BillingDetailsID"].ToString() != "")
                {
                    model.BillingDetailsID = int.Parse(ds.Tables[0].Rows[0]["BillingDetailsID"].ToString());
                }
                model.IntoSectionID = int.Parse(ds.Tables[0].Rows[0]["IntoSectionID"].ToString());
                model.CustomerID = int.Parse(ds.Tables[0].Rows[0]["CustomerID"].ToString());
                if (ds.Tables[0].Rows[0]["IntoSectionDate"].ToString() != "")
                {
                    model.IntoSectionDate = DateTime.Parse(ds.Tables[0].Rows[0]["IntoSectionDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InpuTime"].ToString() != "")
                {
                    model.InpuTime = DateTime.Parse(ds.Tables[0].Rows[0]["InpuTime"].ToString());
                }
                model.IntoSectionType = ds.Tables[0].Rows[0]["IntoSectionType"].ToString();
                model.ImportBank = ds.Tables[0].Rows[0]["ImportBank"].ToString();
                model.ImportBankAccount = ds.Tables[0].Rows[0]["ImportBankAccount"].ToString();
                model.CollectionType = ds.Tables[0].Rows[0]["CollectionType"].ToString();
                model.ReceivingBank = ds.Tables[0].Rows[0]["ReceivingBank"].ToString();
                model.ReceivingBankAccount = ds.Tables[0].Rows[0]["ReceivingBankAccount"].ToString();
                model.ReceivingBankCom = ds.Tables[0].Rows[0]["ReceivingBankCom"].ToString();
                if (ds.Tables[0].Rows[0]["ImportAmount"].ToString() != "")
                {
                    model.ImportAmount = decimal.Parse(ds.Tables[0].Rows[0]["ImportAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndBookDate"].ToString() != "")
                {
                    model.EndBookDate = DateTime.Parse(ds.Tables[0].Rows[0]["EndBookDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputUserID"].ToString() != "")
                {
                    model.InputUserID = int.Parse(ds.Tables[0].Rows[0]["InputUserID"].ToString());
                }
                model.IntoSectionContent = ds.Tables[0].Rows[0]["IntoSectionContent"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BillingDetailsID,IntoSectionID,CustomerID,IntoSectionDate,InpuTime,IntoSectionType,ImportBank,ImportBankAccount,CollectionType,ReceivingBank,ReceivingBankAccount,ReceivingBankCom,ImportAmount,EndBookDate,InputUserID,IntoSectionContent ");
            strSql.Append(" FROM BillingDetails ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return SqlHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BillingDetailsID,IntoSectionID,CustomerID,IntoSectionDate,InpuTime,IntoSectionType,ImportBank,ImportBankAccount,CollectionType,ReceivingBank,ReceivingBankAccount,ReceivingBankCom,ImportAmount,EndBookDate,InputUserID,IntoSectionContent ");
            strSql.Append(" FROM BillingDetails ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return SqlHelper.Query(strSql.ToString());

        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "BillingDetails";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
